<!DOCTYPE html>
<html>
	<head>
	    <title>轮播图片表-列表</title>
	    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
	    <!-- 所有的 css js 资源 -->
	    <link rel="stylesheet" href="https://unpkg.com/element-ui@2.8.2/lib/theme-chalk/index.css">
	    <link rel="stylesheet" href="../../static/sa.css"> 
	    <script src="https://unpkg.com/vue@2.6.10/dist/vue.min.js"></script>
	    <script src="https://unpkg.com/element-ui@2.8.2/lib/index.js"></script>
	    <script src="https://unpkg.com/jquery@3.4.1/dist/jquery.js"></script>
	    <script src="https://www.layuicdn.com/layer-v3.1.1/layer.js"></script>
	    <script src="../../static/sa.js"></script>
		<style type="text/css">
			.el-radio{margin-right: 10px;}
			.td-img{width: 160px; height: 90px; cursor: pointer; vertical-align: middle;}
		</style>
	</head>
	<body>
		<div class="vue-box" style="display: none;" :style="'display: block;'">
			<!-- 表单部分 -->
			<div class="c-panel">
				<h4 class="c-title">检索参数</h4>
				<div class="c-item">
					<label class="c-label">轮播标题：</label>
					<el-input size="mini" v-model="p.title" @keyup.native.enter="f5()"></el-input>
				</div>
				<div class="c-item" style="min-width: 10px;">
					<el-button type="primary" icon="el-icon-search" size="mini" @click="f5">查询</el-button>
				</div>
				<div class="c-item br">
					<label class="c-label">轮播状态：</label>
					<el-radio-group v-model="p.status" class='s-radio-text'>
						<el-radio :label="0">不限</el-radio>
						<el-radio :label="1">显示</el-radio>
						<el-radio :label="2">隐藏</el-radio>
					</el-radio-group>
				</div>
				<!-- ------------- 快捷按钮 ------------- -->
				<div class="fast-btn">
					<el-button size="mini" type="primary" icon="el-icon-plus" @click="add()">新增</el-button>
					<el-button size="mini" type="success" icon="el-icon-edit" @click="update($refs['data-table'].selection[0])"
						:disabled="!$refs['data-table'] || $refs['data-table'].selection.length != 1">修改</el-button>
					<el-button size="mini" type="danger" icon="el-icon-delete" @click="deleteByIds()"
						:disabled="!$refs['data-table'] || $refs['data-table'].selection.length < 1">删除</el-button>
					<el-button size="mini" type="warning" icon="el-icon-download" @click="sa.exportExcel()">导出</el-button>
					<el-button size="mini" type="info"  icon="el-icon-refresh"  @click="sa.f5()">重置</el-button>
				</div>
				<!-- ------------- 数据列表 ------------- -->
				<el-table class="data-table" ref="data-table" :data="dataList" size="small">
					<el-table-column type="selection" width="45px"></el-table-column>
					<el-table-column label="id" prop="id" width="100px"></el-table-column>
					<el-table-column label="图片（点击预览）" width="250px">
						<template slot-scope="s">
							<img :src="s.row.img_src" class="td-img" title="" @click="sa.showImage(s.row.img_src)">
						</template>
					</el-table-column>
					<el-table-column label="标题" prop="title" ></el-table-column>
					<el-table-column label="点击后" width="200px">
						<template slot-scope="s">
							<div v-if="true">
								<p v-if="s.row.action_way == 1">无动作</p>
								<p v-if="s.row.action_way == 2">
									<el-link :href="s.row.action_value" target="_blank">
										打开站外链接：<br>{{s.row.action_value}}
									</el-link>
								</p>
								<p v-if="s.row.action_way == 3">
									<el-link @click="sa.$page.openGoodsInfo(s.row.action_value)">
										打开商品：{{s.row.action_value}}
									</el-link>
								</p>
							</div>
						</template>
					</el-table-column>
					<el-table-column label="点击量" prop="click_count" ></el-table-column>
					<el-table-column label="状态">
						<template slot-scope="s">
							<b style="color: green;" v-if="s.row.status==1">显示</b>
							<b style="color: red;" v-if="s.row.status==2">隐藏</b>
						</template>
					</el-table-column>
					<el-table-column label="创建时间" width="160px">
						<template slot-scope="s">{{sa.forDate(s.row.create_time, 2)}}</template>
					</el-table-column>
					<el-table-column label="排序值" width="100px">
						<template slot-scope="scope">
							<el-input size="mini" v-model="scope.row.sort" style="width: 0px;" v-show="false"></el-input>
							<el-link type="primary" size="mini" @click="sort_add(scope.row)"> 升1</el-link>
							<el-link type="primary" size="mini" @click="sort_top(scope.row)" style="margin-left: 7px;"> 置顶</el-link>
						</template>
					</el-table-column>
					<el-table-column label="操作" width="180px">
						<template slot-scope="s">
							<span @click="update(s.row)">
								<el-button type="primary" class="c-btn" icon="el-icon-edit">修改</el-button>
							</span>
							<span @click="del(s.row)">
								<el-button type="danger" class="c-btn" icon="el-icon-delete">删除</el-button>
							</span>
						</template>
					</el-table-column>
				</el-table>
				<!-- 分页 -->
				<div class="page-box">
					<el-pagination background
						layout="total, prev, pager, next, sizes, jumper" 
						:current-page.sync="p.pageNo" 
						:page-size.sync="p.pageSize" 
						:total="dataCount" 
						:page-sizes="[1, 10, 20, 30, 40, 50, 100]" 
						@current-change="f5()" 
						@size-change="f5()">
					</el-pagination>
				</div>
			</div>
		</div>

        <script>
			var app = new Vue({
				el: '.vue-box',
				data: {
					sa: sa, 	// 超级对象
					p: {		// 查询参数
						title: '',
						type: sa.p('type', 1),		// 控制显示的是哪一个 
						status: 0,
						pageNo: 1,
						pageSize: 10,
					},
					dataCount: 0,
					dataList: [],	// 数据集合
				},
				methods: {
					// 刷新
					f5: function(){
						sa.ajax('/SysSwiper/getList', this.p, function(res) {
							this.dataList = res.data;
							this.dataCount = res.dataCount;
							sa.f5TableHeight();		// 刷新表格高度 
						}.bind(this))
						// sa.$fast.fast_f5(this, '/SysSwiper/getList', isPage);
					},
					// 升1 交换顺序算法哈哈哈
					sort_add: function(data){
						var index = this.dataList.indexOf(data);
						if(index == 0){
							return layer.msg('已经置顶了');	// 已经是第一
						}
						// 获取信息
						var pre_data = this.dataList[index - 1];	// 上一个
						var sort = data.sort;
						var pre_sort = pre_data.sort;
						if(sort == pre_sort){
							return layer.msg('当前项排序值与上一项相同，无法进行此操作', {anim: 6, icon: 5});
						}
						sa.loading('正在交换排序...');
						// 开始交换
						var ajax_1 = sa.ajax('/SysSwiper/updateSort', {id: data.id, sort: pre_sort}, function(res){ }, {msg: null});
						var ajax_2 = sa.ajax('/SysSwiper/updateSort', {id: pre_data.id, sort: sort}, function(res){ }, {msg: null});
						$.when(ajax_1, ajax_2).done(function(){
							// 交换顺序
							data.sort = pre_sort;
							pre_data.sort = sort;
							this.dataList[index] = pre_data;
							this.dataList[index - 1] = data;
							setTimeout(function() {
								layer.msg('升1成功');
							}, 10);
						}.bind(this))
					},
					// 置顶 
					sort_top: function(data){
						var sort = new Date().getTime();
						sa.ajax('/SysSwiper/updateSort', {id: data.id, sort: sort}, function(){
							data.sort = sort;
							sa.arrayDelete(this.dataList, data);
							this.dataList.unshift(data);
							layer.msg('置顶成功');
						}.bind(this))
					},
					// add 
					add: function() {
						sa.showIframe('添加一个', 'sys-swiper-add.html?id=-1&type=' + this.p.type, '600px', '520px')
					},
					// 修改 
					update: function(data) {
						sa.showIframe('修改', 'sys-swiper-add.html?id=' + data.id + '&type=' + this.p.type, '600px', '520px')
					},
					// 删除
					del: function(data) {
						sa.confirm('是否删除，此操作不可撤销', function() {
							sa.ajax('/SysSwiper/delete?id=' + data.id, function(res) {
								sa.arrayDelete(this.dataList, data);
								sa.ok2('删除成功');
								sa.f5TableHeight();		// 刷新表格高度 
							}.bind(this))
						}.bind(this));
					},
					// 批量删除
					deleteByIds: function() {
						// 获取选中元素的id列表
						let selection = this.$refs['data-table'].selection;
						let ids = sa.getArrayField(selection, 'id');
						// 提交删除 
						sa.confirm('是否批量删除选中数据？此操作不可撤销', function() {
							sa.ajax('/SysSwiper/deleteByIds', {ids: ids.join(',')}, function(res) {
								sa.arrayDelete(this.dataList, selection);
								sa.ok('删除成功');
								sa.f5TableHeight();		// 刷新表格高度 
							}.bind(this))
						}.bind(this));
					},
				},
				created: function(){
					this.f5(); 
					sa.onInputEnter();
				}
			});
		</script>
	</body>
</html>